package com.example.demo.service.Impl;

import com.example.demo.domain.Order;
import com.example.demo.repository.OrderMapper;
import com.example.demo.service.ExpressService;
import com.example.demo.service.GoodsService;
import com.example.demo.service.OrderService;
import com.example.demo.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
public class OrderServiceImpl implements OrderService {

    @Autowired
    private OrderMapper orderMapper;

    @Autowired
    private UserService userService;

    @Autowired
    private GoodsService goodsService;

    @Autowired
    private ExpressService expressService;

    @Override
    @Transactional(rollbackFor = {RuntimeException.class, Error.class})
    public Order getOrderById(Integer id) {
        return orderMapper.getOrderById(id);
    }

    @Override
    @Transactional(rollbackFor = {RuntimeException.class, Error.class})
    public List<Order> getCartOrderByUid(Integer uid, Integer start, Integer end) {
        List<Order> orders=orderMapper.getCartOrderByUid(uid,start,end);
        for(int i=0;i<orders.size();i++){
            orders.get(i).setUser(userService.getUserById(orders.get(i).getUid()));
            orders.get(i).setGoods(goodsService.getGoodsById(orders.get(i).getGid()));
            orders.get(i).setExpress(expressService.getExpressById(orders.get(i).getEid()));
        }
        return orders;
    }

    @Override
    @Transactional(rollbackFor = {RuntimeException.class, Error.class})
    public List<Order> getNonCartOrderByUid(Integer uid, Integer start, Integer end) {
        List<Order> orders=orderMapper.getNonCartOrderByUid(uid,start,end);
        for(int i=0;i<orders.size();i++){
            orders.get(i).setUser(userService.getUserById(orders.get(i).getUid()));
            orders.get(i).setGoods(goodsService.getGoodsById(orders.get(i).getGid()));
            orders.get(i).setExpress(expressService.getExpressById(orders.get(i).getEid()));
        }
        return orders;
    }

    @Override
    @Transactional(rollbackFor = {RuntimeException.class, Error.class})
    public List<Order> getOrder(Integer state) {
        return orderMapper.getOrder(state);
    }

    @Override
    @Transactional(rollbackFor = {RuntimeException.class, Error.class})
    public List<Order> getSearch(String searchText, Integer state) {
        return orderMapper.getSearch(searchText,state );
    }

    @Override
    @Transactional(rollbackFor = {RuntimeException.class, Error.class})
    public List<Order> getHistoryOrders() {
        return orderMapper.getHistoryOrders();
    }

    @Override
    @Transactional(rollbackFor = {RuntimeException.class, Error.class})
    public List<Order> getHistoryOrderSearch(String searchText) {
        return orderMapper.getHistoryOrderSearch(searchText);
    }


    @Override
    @Transactional(rollbackFor = {RuntimeException.class, Error.class})
    public Integer getIdByOrder(Order order) {
        return orderMapper.getIdByOrder(order.getUid(),order.getTimes() ,order.getGid() ,order.getGoodsNum());
    }

    @Override
    @Transactional(rollbackFor = {RuntimeException.class, Error.class})
    public void saveOrderState(Integer id, Integer state) {
       orderMapper.saveOrderState(id,state);
    }


    @Override
    @Transactional(rollbackFor = {RuntimeException.class, Error.class})
    public Order getBaseOrderById(Integer id) {
        return orderMapper.getBaseOrderById(id);
    }


    @Override
    @Transactional(rollbackFor = {RuntimeException.class, Error.class})
    public void addOrder(Order order) {
        orderMapper.addOrder(order);
    }

    @Override
    @Transactional(rollbackFor = {RuntimeException.class, Error.class})
    public void delOrderById(Integer id) {
        orderMapper.delOrderById(id);
    }

    @Override
    @Transactional(rollbackFor = {RuntimeException.class, Error.class})
    public void saveOrder(Order order) {
        orderMapper.saveOrder(order);
    }
}
